<template>
    <div class="userBox">
        <div class="usersTitle">
            <div class="titleText">用户管理</div>
            <div class="titlesBrn">
                <el-button class="titlesBrn" type="primary" size="mini" @click="toAddUsers">新增用户</el-button>
            </div>
        </div>
        <el-table :data="pageData" style="width: 100%" header-row-class-name="elTableStyle">
            <el-table-column prop="name" label="用户名"> </el-table-column>
            <el-table-column prop="account" label="登陆账号"> </el-table-column>
            <el-table-column prop="phone" label="手机号码"> </el-table-column>
            <el-table-column prop="photoUrl" label="头像">
                <template slot-scope="scope">
                    <el-image class="userHeadImg" :src="scope.row.photoUrl" fit="cover"></el-image>
                </template>
            </el-table-column>
            <!-- <el-table-column prop="planType" label="任务类型">
                <template slot-scope="scope">
                    <span v-if="scope.row.planType == 'TESTTASK'">测试</span>
                    <span v-if="scope.row.planType == 'DEMANDTASK'">需求</span>
                    <span v-if="scope.row.planType == 'COTASK'">协办</span>
                    <span v-if="scope.row.planType == 'BUGTASK'">BUG</span>
                </template>
            </el-table-column> -->
            <el-table-column prop="permission" label="权限">
                <template slot-scope="scope">
                    <span v-if="scope.row.permission == '2'">超级管理员</span>
                    <span v-if="scope.row.permission == '1'">管理员</span>
                    <span v-if="scope.row.permission == '0'">普通用户</span>
                </template>
            </el-table-column>
            <el-table-column prop="address" label="操作">
                <template slot-scope="scope">
                    <el-button type="text" @click="handlClick(scope.row, '1')">修改</el-button>
                    <el-button type="text" @click="handlClick(scope.row, '0')">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- <pagination :totalNum="taskData.totalNum" @paginaClick="paginaClickTable"></pagination> -->
    </div>
</template>
<script lang="ts">
export default {
    data() {
        return {
            pageData: []
        }
    },
    mounted() {
        ;(this as any).getUserList()
    },
    methods: {
        paginaClickTable(val: string | number) {
            console.log(val)
        },
        toAddUsers() {
            if ((this as any).$isAdmin()) {
                ;(this as any).$router.push('/users/adduser')
            } else {
                ;(this as any).$alert('你无权进行此操作！请联系管理员')
            }
        },
        getUserList() {
            ;(this as any).$request.get('/api/oa/users/list').then((res: any) => {
                if (res.code == 200) {
                    ;(this as any).pageData = res.data
                }
            })
        },
        handlClick(row: any, type: string) {
            let userInfo = JSON.parse((sessionStorage as any).getItem('loginMsg'))
            // 普通用户修改，只能修改自己账号--当用户角色不为管理员，修改自己以外的账号时：
            if (row.id != userInfo.id && !(this as any).$isAdmin()) {
                ;(this as any).$alert('你无权进行此操作！请联系管理员')
                return false
            }
            // 无法修改同为管理员账号！--当用户角色为管理员，修改自己以外的管理员账号时：
            if ((this as any).$isAdmin() && row.permission == '1' && row.id != userInfo.id && type != '0') {
                // if(){

                // }
                ;(this as any).$alert('无法操作同为管理员账户！')
                return false
            }
            // 当用户操作删除或者删除对象为管理员时：
            if ((row.id == userInfo.id && type == '0') || (row.permission == '1' && row.id != userInfo.id)) {
                if (row.id == userInfo.id) {
                    ;(this as any).$alert('无法删除自己！')
                } else if (row.permission == '1') {
                    ;(this as any).$alert('无法删除同为管理员账户！')
                }
                return false
            }
            if (type == '0') {
                ;(this as any)
                    .$confirm('确定要删除该用户?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    })
                    .then(() => {
                        ;(this as any).$request.postForm('/api/oa/users/delete', { userId: row.id }).then((res: any) => {
                            if (res.code == 200) {
                                ;(this as any).$message.success('删除成功')
                                ;(this as any).getUserList()
                            }
                        })
                    })
                    .catch(() => {
                        ;(this as any).$message({
                            type: 'info',
                            message: '已取消删除'
                        })
                    })
            } else if (type == '1') {
                // 修改
                ;(this as any).$router.push(`/users/editoruser/${row.id}`)
            }
        }
    }
}
</script>
<style lang="scss" scoped>
.userBox {
    background: #ffffff;
    padding: 20px;
    .usersTitle {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #f5f5f5;
        margin-bottom: 25px;
        padding-bottom: 15px;
        .titleText {
            font-weight: 600;
        }
        .titlesBrn {
            width: 100px;
        }
    }
    .userHeadImg {
        width: 100px;
        height: 100px;
        border-radius: 5px;
        cursor: pointer;
    }
}
</style>
<style>
.userBox .elTableStyle {
    color: #333333;
}
.userBox .elTableStyle th {
    background: #f5f5f5;
    text-align: center;
}
.userBox table tr td .cell {
    text-align: center !important;
}
</style>
